﻿using Avalonia.Data.Converters;
using Avalonia.Media.Imaging;
using AvaloniaApplication1.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Text;
using mesign.pc.Helper;

namespace AvaloniaApplication1.Converters
{
    public class EnumIconConverter : IValueConverter
    {
        private static readonly object m_locker = new object();

        private static Dictionary<SignIcon, IBitmap> m_DictSignImages = new Dictionary<SignIcon, IBitmap>();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                return SystemIcons.Application.ToBitmap();
                SignIcon signIcon = (SignIcon)value;
                IBitmap iBitmap = GetBitmap(signIcon);
                return iBitmap;
            }
            catch (Exception e)
            {
                //这里最好return 一个DefultIcon
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        public static IBitmap GetBitmap(SignIcon signIcon)
        {
            if (m_DictSignImages.TryGetValue(signIcon, out IBitmap bitmap))
            {
                return bitmap;
            }

            switch (signIcon)
            {
                case SignIcon.Defult:
                    bitmap = ImageHelper.GetBitmap("IconTreeHead.png");
                    break;
                case SignIcon.Rsa:
                    bitmap = ImageHelper.GetBitmap("IconTreeHeadRsa.png");
                    break;
            }

            if (bitmap != null)
            {
                lock (m_locker)
                {
                    if (!m_DictSignImages.ContainsKey(signIcon))
                    {
                        m_DictSignImages.Add(signIcon, bitmap);

                    }
                }
            }
            return bitmap;
        }
    }
}
